package com.example.mq.mqserver.datacenter;

import com.example.mq.MqApplication;
import com.example.mq.mqserver.core.Binding;
import com.example.mq.mqserver.core.Exchange;
import com.example.mq.mqserver.core.ExchangeType;
import com.example.mq.mqserver.core.MSGQueue;
import com.example.mq.mqserver.mapper.MetaMapper;

import java.io.File;
import java.util.List;
import java.util.Queue;

/**
 * @Description: 通过这个类来整合上述的数据库操作
 * @Author: liudong
 */

public class DataBaseManager {
    //要从Spring中拿到现成的对线
    private MetaMapper metaMapper;

    //针对数据库进行初始化,=建库建表+Charon一些默认的数据
    public void init() {
        //手动获取到MataMapper
        metaMapper = MqApplication.context.getBean(MetaMapper.class);

        if (!checkDBEcists()) {
            //数据库不存在就进行建库建表
            //创建数据库目录
            File dataDir = new File("./data");
            dataDir.mkdirs();
            //创建数据库
            createTable();
            //插入默认数据
            createDefaultData();
            System.out.println("[DataBaseMannager] 数据库初始化完成！");
        } else {
            //数据库已经存在，啥都不用做
            System.out.println("[DataBaseManager] 数据库已经存在！");
        }

    }

    //删除数据库文件
    public void deleteDB() {
        File file = new File("./data/meta.db");
        boolean ret = file.delete();
        if (ret) {
            System.out.println("删除数据库成功");
        } else {
            System.out.println("删除数据库失败");
        }
        //删除目录
        File dataDir = new File("./data");
        //使用delete删除时需要保证目录是空的
        ret = dataDir.delete();
        if (ret) {
            System.out.println("删除目录成功");
        } else {
            System.out.println("删除目录失败");
        }
    }

    private boolean checkDBEcists() {
        File file = new File("./data/meta.db");
        if (file.exists()) {
            return true;
        }
        return false;
    }

    //这个方法用来建表
    //建库不需要手动执行（不用手动创建mata.db文件）
    //首次执行数据库操作时，会自动创建mata.db文件
    private void createTable() {
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        metaMapper.createBindingTable();
        System.out.println("[DataBaseManager] 创建表完成！");
    }

    //创建默认数据就是创建默认交换机
    //RubbitMQ中有个设定：带有一个匿名的交换机，类型是DIRECT
    private void createDefaultData() {
        //构造一个默认的交换机
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        metaMapper.insertExchange(exchange);
        System.out.println("[DataBaseManager] 创建交换机完成！");
    }

    //其他数据库操作，也在这个类中封装
    public void insertExchange(Exchange exchange) {
        metaMapper.insertExchange(exchange);
    }

    public void deleteExchange(String exchangeName) {
        metaMapper.deleteExchange(exchangeName);
    }

    public List<Exchange> selectAllExchangs() {
        return metaMapper.selectAllExchanges();
    }

    public void insertQueue(MSGQueue queue) {
        metaMapper.insertQueue(queue);
    }

    public void deleteQueue(String queue) {
        metaMapper.deleteQueue(queue);
    }

    public List<MSGQueue> selectAllQueues() {
        return metaMapper.selectAllQueues();
    }

    public void insertBinding(Binding binding) {
        metaMapper.insertBinding(binding);
    }

    public void deleteBinding(Binding binding) {
        metaMapper.deleteBinding(binding);
    }

    public List<Binding> selectAllBindings() {
        return metaMapper.selectAllBindings();
    }
}
